<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
        Object
            Person人 属性：姓名性别年龄 方法：吃
                Worker工人 属性：职业zhiye 方法：劳动work()
                    Coder码农 属性：语言language 方法：编程code()
                    Teacher老师 属性：课程course 方法：教学teach()
        造一个码农实例 令其吃劳动教学
        */

        /* Person人 属性：姓名性别年龄 方法：吃 */
        function Person(name, age, gender) {
            this.name = name
            this.age = age
            this.gender = gender
        }
        Person.prototype.eat = function (food) {
            console.log(`${this.name}正在食用${food}`);
        }
        Person.prototype.sleep = function () {
            console.log(`${this.name}正在躺尸`);
        }



        /* Worker工人 属性：职业zhiye 方法：劳动work() */
        function Worker(name, age, gender, zhiye) {
            Person.call(this, name, age, gender)
            this.zhiye = zhiye
        }
        /* 取一个Person实例当老婆 则所有Worker实例会吃会睡 */
        Worker.prototype = new Person()
        Worker.prototype.work = function () {
            console.log("劳动方法请子类自行实现");
        }



        /* Coder码农 属性：语言language 方法：编程code() */
        function Coder(name, age, gender, language) {
            Worker.apply(this, [name, age, gender, "码农"])
            this.language = language
        }
        /* 取一个Worker实例当老婆 则所有Coder实例会吃会睡会工作 */
        Coder.prototype = new Worker()
        Coder.prototype.code = function () {
            console.log(`码农${this.name}正在使用${this.language}语言创作BUG`);
        }
        /* 不满意父类对工作方法work的实现，重写之 */
        Coder.prototype.work = function () {
            this.code()
        }


        /* Teacher老师 属性：课程course 方法：教学teach() */
        function Teacher(name, age, gender, course) {
            Worker.apply(this, [name, age, gender, "老师"])
            this.course = course
        }
        /* 取一个Worker实例当老婆 则所有Teacher实例会吃会睡会工作 */
        Teacher.prototype = new Worker()
        Teacher.prototype.teach = function () {
            console.log(`老师${this.name}正在教授《${this.course}》`);
        }
        /* 不满意父类对工作方法work的实现，重写之 */
        Teacher.prototype.work = function () {
            this.teach()
        }

        /* 业务逻辑 */
        const workers = []
        workers.push(
            new Coder("bill", 80, "男", "Basic/C")
        )
        workers.push(
            new Teacher("乔布梭", 18, "男", "JavaScript从入门到住院")
        )

        /* 使用共性 */
        // workers.forEach(
        //     function(worker){
        //         worker.work()//具体工作方法各不相同
        //     }
        // )
        // workers.forEach(worker => worker.work())

        /* 测试继承来的方法 */
        workers.forEach(worker => {
            worker.eat("汉堡")
        })

    </script>
</body>

</html>